package com.psq.core.common.model

open class SSOApiResponse<T>(
    val data: T? = null,
    val code: String = "",
    val msg: String = "",
) {
    class Error<T>(errorMessage: String) : SSOApiResponse<T>(null, "", errorMessage)
}

fun <T> SSOApiResponse<T>.whenSuccess(success: Success<T>): SSOApiResponse<T> {
    if (code == "0000") {
        success(this.data)
    }
    return this
}

fun <T> SSOApiResponse<T>.whenFailure(failure: Failure): SSOApiResponse<T> {
    if (code != "0000") {
        failure(code, msg)
    }
    return this
}

fun <T> SSOApiResponse<T>.`do`(
    success: Success<T>,
    failure: Failure = { _, _ -> }
): SSOApiResponse<T> {
    whenSuccess(success)
    whenFailure(failure)
    return this
}